
#import "NewestController.h"
#import "NewsCell.h"
#import "UIImageView+WebCache.h"
#import "NewestAticleCtr.h"
#import "ShoolDataModel.h"
#import "NewstModel.h"
#import "NEWAticListModel.h"
#import "NewRecomModel.h"
#import "CGFdbmBase.h"
#import "Recomment.h"
#import "PageInfoModel.h"
#import "VideoDescrib.h"
#import "RootTableview.h"
#import "MJRefresh.h"
@interface NewestController ()<UITableViewDataSource,UITableViewDelegate>
{
    RootTableview *_tableview;
    CGFloat _cellHeight;
    NSMutableArray *_dataAry;
    ShoolDataModel *_model;
    NewstModel *_newstModel;
    NEWAticListModel *_AticModel3;
    NewRecomModel *_recomModel;
    PageInfoModel *_infoModel;
    Recomment *_recommentView;
    NSMutableArray *_listAry;
    NSMutableArray *_recomAry;
}

@end

@implementation NewestController
-(void)viewWillAppear:(BOOL)animated{
    self.app.myTabBar.tabBar.hidden = NO;
    self.navigationController.navigationBarHidden = YES;
    
    }

-(void)GetAFNtWhithPage:(NSInteger )page{

    [[Manager sharedManager]AchiveDataOfSchoolTypeid:0 Page:page SQLTablePath:@"Newest.db" tableName:@"Newest" AndDataAry:_dataAry AndResult:^(id list, id recomment, id pageInfo,id fourth_classify ) {
        if (_recomAry.count<=0) {
            [_recomAry addObjectsFromArray:recomment];
        }
        
        [_listAry addObjectsFromArray:list];
        _infoModel = pageInfo;
        
        [self HeadOfTableview];
        [_tableview.header endRefreshing];
        [_tableview.footer endRefreshing];
        _tableview.isShowImage = YES;
        [_tableview reloadData];
        
    } AError:^(DIEError *error) {
        [Manager GetHudWhithText:@"网络加载失败" AndFatherView:self.view];
        [self DataFromSQL];
        [_tableview.header endRefreshing];
        [_tableview.footer endRefreshing];
        _tableview.isShowFile = YES;
        [_tableview reloadData];
    }];
    
}

-(void)DataFromSQL{
    
    [_dataAry removeAllObjects];
    [_recomAry removeAllObjects];
    [_listAry removeAllObjects];
    
    [CGFdbmBase OpenSQlWhiPathName:@"Newest.db"];
   
    [_dataAry addObjectsFromArray:[CGFdbmBase ListWhith:@"Newest" dicName:@"data"]];
    _model = [[ShoolDataModel alloc]initWithDictionary:_dataAry.firstObject error:nil];
    [_listAry addObjectsFromArray:_model.article_list];
    if (_recomAry.count<=0) {
        [_recomAry addObjectsFromArray:_model.recommend];
    }
    [_tableview reloadData];
    [self HeadOfTableview];
}

#pragma mark--下拉刷新
- (void)loadNewData {
    [_tableview.footer endRefreshing];
    
    [_dataAry removeAllObjects];
    [_recomAry removeAllObjects];
    [_listAry removeAllObjects];
    [self GetAFNtWhithPage:1];
}
#pragma mark--上拉加载
- (void)loadMoreData {
   
    [_tableview.header endRefreshing];
    if (![_infoModel.nextPage isEqualToString:_infoModel.pageCount]) {
        [self GetAFNtWhithPage:_infoModel.nextPage.integerValue];
    }else{
        [_tableview.footer noticeNoMoreData];
    }
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _dataAry = [NSMutableArray array];
    _listAry = [NSMutableArray array];
    _recomAry =[NSMutableArray array];
    [self CrectTableview];
    [self GetAFNtWhithPage:1];
}

-(void)CrectTableview{
    _tableview = [[RootTableview alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-44) style:UITableViewStylePlain];
    [self.view addSubview:_tableview];
    
    __weak typeof(self) weakSelf = self;
    _tableview.TapBlok = ^{[weakSelf loadNewData];};
    _tableview.dataSource = self;
    _tableview.delegate = self;
     _tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    [_tableview registerClass:[NewsCell class] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:_tableview];
    
    _tableview.header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    _tableview.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];

}

-(void)HeadOfTableview{
    
    _recommentView = [[Recomment alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.23)];

     _recommentView.titleAry = [_recomAry copy];
    __weak typeof(self) weakself = self;
    
    _recommentView.Click = ^(NSNumber *skin,NSString *AticID){
        
        if (skin.integerValue == 1){
            VideoDescrib *video = [VideoDescrib new];
            video.AticleID = AticID;
            [weakself.navigationController  pushViewController:video animated:YES];
        }else{
            NewestAticleCtr *aticle = [NewestAticleCtr new];
            aticle.AticleID =AticID;
            [weakself.navigationController pushViewController:aticle animated:YES];
        }
    };
    _tableview.tableHeaderView = _recommentView;

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 0;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _listAry.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return _cellHeight;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (_listAry.count>0) {
        cell.model =_listAry[indexPath.row];
        _cellHeight = cell.cellHight;
    }
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (_listAry.count!=0) {
        NEWAticListModel  *Model3 = _listAry[indexPath.row];
        if (Model3.skin.integerValue == 1){
            VideoDescrib *video = [VideoDescrib new];
            video.AticleID = Model3.AtcID;
            [self.navigationController  pushViewController:video animated:YES];
        }else{
            NewsCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            NewestAticleCtr *aticle = [NewestAticleCtr new];
            aticle.AticleID = cell.AticleID;
            [self.navigationController pushViewController:aticle animated:YES];
        }
    }
}

-(void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];//即使没有显示在window上，也不会自动的将self.view释放。
    if ([self.view window] == nil) {
        self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
    }
}
@end